using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Clanta.Core.Domain;
using System.Threading;
using Clanta.Web.BusinessLogic;
using NHibernate;
using System.Collections.Generic;

namespace Clanta.Web.Controls
{
    public partial class ConferenceListWebPart : WebPartUserControl
    {
        #region Properties

        /// <summary>
        /// Returns the customer associated with the user.
        /// </summary>
        private Customer Customer
        {
            get
            {
                Customer customer = Cache["Customer"] as Customer;
                if (customer == null)
                {

                    ISession session = NHibernateHelper.GetCurrentSession();

                    IList<Customer> customers =
                        session.CreateQuery("select c from Customer c left outer join c.Users as u where u in elements(c.Users) and u.UserName = :UserName")
                            .SetString("UserName", Thread.CurrentPrincipal.Identity.Name)
                            .List<Customer>();

                    //IList<Customer> customers = session.Find("from Customer c");

                    //IList<Customer> customers =
                    //session.GetNamedQuery("GetCustomerByUserName")
                    //    .SetString("ApplicationName", Membership.ApplicationName)
                    //    .SetString("UserName", Thread.CurrentPrincipal.Identity.Name)
                    //    .List<Customer>();
                    customer = customers[0];
                    Cache["Customer"] = customer;
                }
                return customer;
            }
        }

        #endregion

        #region Public Methods

        public void ShowConferencesForCustomer(Customer customer)
        {

        }

        #endregion

        #region Web Part Life Cycle

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                WebPart.Title = "Konferencer";
                List<Conference> conferences = DaoFactory.ConferenceDao.GetConferencesByCustomerId(Customer.Id);
                ConferencesGridView.DataSource = conferences;
                ConferencesGridView.DataBind();
            }
        }

        #endregion

        #region Event Handlers

        protected void ConferencesGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            long conferenceId = (long)ConferencesGridView.SelectedDataKey.Value;
            OnConferenceSelected(new ConferenceSelectedEventArgs(conferenceId));
        }

        #endregion

        #region Events

        public event EventHandler<ConferenceSelectedEventArgs> ConferenceSelected;

        protected void OnConferenceSelected(ConferenceSelectedEventArgs e)
        {
            if (ConferenceSelected != null)
            {
                ConferenceSelected(this, e);
            }
        }

        #endregion
    }
}